package com.hs.panghu.dao.imp;

import com.hs.panghu.dao.CourierStationDao;
import com.hs.panghu.entity.CourierStation;
import com.hs.panghu.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CourierStationDaoImpl implements CourierStationDao {
    @Override
    public int insertCourierStation(CourierStation cor) {
        String sql = "insert into courierStation(addTime,courierStationAddress," +
                "courierStationCommunityId,courierStationCommunityName," +
                "courierStationImage,courierStationName) values(now(),?,?,?,?,?)";
        Object object[] = {
                cor.getCourierStationAddress(),
                cor.getCourierStationCommunityId(),
                cor.getCourierStationCommunityName(),
                cor.getCourierStationImage(),
                cor.getCourierStationName()
        };
        int row = JDBCDruidUtil.update(sql, object);
        return row;
    }

    @Override
    public int updateCourierStation(CourierStation courierStation) {
        String sql = "update courierStation set courierStationAddress=?,"+
                "courierStationCommunityId=?,courierStationImage=?,courierStationName=?" +
                "where courierStationId=?";
        Object object[] = {
                courierStation.getCourierStationAddress(),
                courierStation.getCourierStationCommunityId(),
                courierStation.getCourierStationImage(),
                courierStation.getCourierStationName(),
                courierStation.getCourierStationId()
        };
        int row = JDBCDruidUtil.update(sql, object);
        return row;
    }

    @Override
    public int upOrDownCourierStation(CourierStation courierStation) {
        String sql = "update courierStation set courierStationStatus=? where courierStationId=?";
        Object object[] = {
                courierStation.getCourierStationStatus(),
                courierStation.getCourierStationId()
        };
        int update = JDBCDruidUtil.update(sql, object);

        return update;
    }

    @Override
    public CourierStation selectCourierStation(Integer courierStationId) {
        return null;
    }

    @Override
    public List<CourierStation> selectCourierStations(Map<String, String> map) {
        List<CourierStation> list = new ArrayList<>();
        // 动态sql和动态数组
        StringBuffer sb = new StringBuffer("select * from courierstation where 1=1 ");
        List param = new ArrayList();
        // 判断name是否有数据，如果有，则拼接
        String name = map.get("courierStationName");
        if(name !=null && !"".equals(name)){
            sb.append(" and courierStationName like ? ");
            param.add("%"+name+"%");
        }
        // 判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if(startTime!=null && !"".equals(startTime)){
            sb.append(" and  addTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
        //分页处理
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize = Integer.valueOf(map.get("pageSize"));
        sb.append(" order by courierStationId desc limit ?,? ");
        param.add( (pageNum-1)*pageSize );
        param.add(pageSize);

        String sql = sb.toString();
        Object object[] = param.toArray();
//        System.out.println(sql);
        ResultSet resultSet = JDBCDruidUtil.query(sql,object);
        try {
            while (resultSet.next()){
                CourierStation courierStation = new CourierStation(
                        resultSet.getDate(1),
                        resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getInt(4),
                        resultSet.getString(5),
                        resultSet.getString(6),
                        resultSet.getInt(7),
                        resultSet.getString(8),
                        resultSet.getString(9),
                        resultSet.getInt(10),
                        resultSet.getString(11),
                        resultSet.getInt(12),
                        resultSet.getInt(13)
                );
                list.add(courierStation);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCDruidUtil.close();
        }
        return list;
    }

    @Override
    public int selectCourierStationCount(Map<String, String> map) {
        int count = 0;
        // 动态sql和动态数组
        StringBuffer sb = new StringBuffer("select count(1) from courierstation where 1=1 ");
        List param = new ArrayList();
        // 判断name是否有数据，如果有，则拼接
        String name = map.get("courierStationName");
        if(name!=null && !"".equals(name)){
            sb.append(" and courierStationName like ? ");
            param.add("%"+name+"%");
        }
        // 判断日期是否存在
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if(startTime!=null && !"".equals(startTime)){
            sb.append(" and  addTime between ? and ? ");
            param.add(startTime);
            param.add(endTime);
        }
        String sql = sb.toString();
        Object object[] = param.toArray();
        ResultSet resultSet = JDBCDruidUtil.query(sql, object);
        try {
            if(resultSet.next()){
                count = resultSet.getInt(1);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCDruidUtil.close();
        }
        return count;
    }
}
